package com.ysz.redisson.task;

import com.ysz.redisson.config.RedisEncapsulation;

import java.util.List;

/**
 * @ClassName MyTask
 * @Description TODO
 * @Data 11:32
 * @Version 1.0
 * @Author ysz
 */
public class ExecutorLua {

    public static Long executorLuaLock(List<String> keys, long leaseTimeMillis, String lockName) {
        String lua = "if (redis.call('exists', KEYS[1]) == 0) then " +
                "redis.call('hset', KEYS[1], ARGV[2], 1); " +
                "redis.call('pexpire', KEYS[1], ARGV[1]); " +
                "return 1; " +
                "end; " +
                "return 2; ";
        return RedisEncapsulation.execute(Long.class, lua, keys, leaseTimeMillis, lockName);
    }


    public static Long executorLuaRenew(List<String> keys, long leaseTimeMillis, String lockName) {
        String lua = "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
                "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
                "redis.call('pexpire', KEYS[1], ARGV[1]); " +
                "return 1; " +
                "end; " +
                "return 2; ";
        return RedisEncapsulation.execute(Long.class, lua, keys, leaseTimeMillis, lockName);
    }
}

